package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetUserOuterAccountListBean;
import com.rjwh.dingdong.client.bean.localbean.GetUserOuterAccountListBean;
import com.rjwh.dingdong.client.bean.localbean.UserOuterAccountListData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.MoreBindSocialAccountModel;
import com.rjwh.dingdong.client.mvp.presenter.IMoreBindSocialAccountPresenter;
import com.rjwh.dingdong.client.mvp.view.MoreBindSocialAccountView;
import com.rjwh.dingdong.client.util.ToastUtil;

/**
 * @author Kay
 * @ClassName MoreBindSocialAccountPresenter
 * @Description
 * @date 2016-10-19
 * @history 2016-10-19 author: description:
 */

public class MoreBindSocialAccountPresenter extends BasePresenter<MoreBindSocialAccountView> implements IMoreBindSocialAccountPresenter {

    private MoreBindSocialAccountModel model;

    public MoreBindSocialAccountPresenter(Context context, MoreBindSocialAccountView mMvpView) {
        super(context, mMvpView);
        model = new MoreBindSocialAccountModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            getMvpView().dissmissLoading();
            switch (businessType) {
                case NetConstant.GETUSEROUTERACCOUNTLIST:
                    ResGetUserOuterAccountListBean resGetUserOuterAccountListBean = (ResGetUserOuterAccountListBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetUserOuterAccountListBean != null && resGetUserOuterAccountListBean.getData() != null) {
                        GetUserOuterAccountListBean data = resGetUserOuterAccountListBean.getData();
                        UserOuterAccountListData weixin = data.getWeixin();
                        UserOuterAccountListData qq = data.getQq();
                        UserOuterAccountListData weibo = data.getWeibo();
                        UserOuterAccountListData xiaomi = data.getXiaomi();
                        if (weixin != null && qq != null && weibo != null && xiaomi != null) {
                            getMvpView().loadData(weixin, qq, weibo, xiaomi);
                        }

                    } else if (mAsResult.getResultMsg() != null) {
                        ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                    }
                    break;

                case NetConstant.USEROUTERSYSUNBIND:
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS) {
                        getMvpView().setData(0);
                        ToastUtil.showToast(getContext(), "解绑成功!");
                    } else if (mAsResult.getResultMsg() != null) {
                        ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                    }
                    break;

                case NetConstant.USERBINDOUTERACCOUNT:
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS) {
                        getMvpView().setData(1);
                        ToastUtil.showToast(getContext(), "绑定成功!");
                    } else if (mAsResult.getResultMsg() != null) {
                        ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

    @Override
    public void doGetUserOuterAccountList() {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doGetUserOuterAccountList();
        }
    }

    @Override
    public void doUserOuterSysUnBind(String apptype, String uid) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doUserOuterSysUnBind(apptype, uid);
        }
    }

    @Override
    public void doUserBindOuterAccount(String uid, String nickname, String avatar, String apptype) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doUserBindOuterAccount(uid, nickname, avatar, apptype);
        }
    }
}
